package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.dao.AdminInfoMapper;
import com.board.dao.NoticeMapper;
import com.board.entity.AdminInfo;
import com.board.entity.Notice;
import com.board.exception.NoticeException;
import com.board.param.AddNoticeParam;
import com.board.service.NoticeService;
import com.board.util.PageInfo;
import com.board.util.StateDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 维修工程的公告表 服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private AdminInfoMapper adminInfoMapper;

    @Override
    public Notice addNotice(AddNoticeParam param, Integer adminId) {
        //查询发布人的信息
        AdminInfo adminInfo = adminInfoMapper.selectById(adminId);
        Notice notice = BeanUtil.toBean(param, Notice.class);
        notice.setNoticeAuthor(adminInfo.getAdminUser());
        //公告默认下架状态
        notice.setNoticeState(StateDate.NOTICE_STATE_DOWN);
        notice.setNoticeTime(LocalDateTime.now().toString());
        notice.setSeeTime(0);
        noticeMapper.insert(notice);
        return notice;
    }

    @Override
    public Notice showNotice(Integer noticeId) {
        //先查询公告是否存在
        Notice notice = noticeMapper.selectById(noticeId);
        if (notice==null) throw new NoticeException(500,"公告不存在");
        //已经删除的公告不能展示
        if (notice.getNoticeState()==StateDate.NOTICE_STATE_DELETE){
            throw new NoticeException(500,"公告不存在");
        }
        if (notice.getNoticeState()==StateDate.NOTICE_STATE_SHOW){
            throw new NoticeException(500,"公告不能重复展示");
        }
        //可以进行展示
        notice.setNoticeState(StateDate.NOTICE_STATE_SHOW);
        noticeMapper.updateById(notice);
        return notice;
    }

    @Override
    public Notice downNotice(Integer noticeId) {
        //先查询公告是否存在
        Notice notice = noticeMapper.selectById(noticeId);
        if (notice==null) throw new NoticeException(500,"公告不存在");
        //已经删除的公告不能展示
        if (notice.getNoticeState()==StateDate.NOTICE_STATE_DELETE){
            throw new NoticeException(500,"公告不存在");
        }
        if (notice.getNoticeState()==StateDate.NOTICE_STATE_DOWN){
            throw new NoticeException(500,"公告不能重复隐藏");
        }
        //可以进行隐藏
        notice.setNoticeState(StateDate.NOTICE_STATE_DOWN);
        noticeMapper.updateById(notice);
        return notice;
    }

    @Override
    public void deleteNotice(Integer noticeId) {
        //先查询公告是否存在
        Notice notice = noticeMapper.selectById(noticeId);
        if (notice==null) throw new NoticeException(500,"公告不存在");
        //已经删除的公告不能重复删除
        if (notice.getNoticeState()==StateDate.NOTICE_STATE_DELETE){
            throw new NoticeException(500,"公告不能重复删除");
        }
        //可以进行删除
        notice.setNoticeState(StateDate.NOTICE_STATE_DELETE);
        noticeMapper.updateById(notice);
    }

    @Override
    public Notice findNoticeById(Integer noticeId) {
        Notice notice = noticeMapper.selectById(noticeId);
        if (notice==null) throw new NoticeException(500,"公告不存在");
        return notice;
    }

    @Override
    public PageInfo<Notice> userFindNotice(Integer pageNum, Integer pageSize) {
        Page<Notice> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("notice_id");
        wrapper.eq("notice_state",StateDate.NOTICE_STATE_SHOW);
        noticeMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }

    @Override
    public PageInfo<Notice> adminFindNotice(String noticeTitle, Integer pageNum, Integer pageSize) {
        Page<Notice> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("notice_id");
        wrapper.ne("notice_state",StateDate.NOTICE_STATE_DELETE);
        if (noticeTitle!=null&&noticeTitle!=""){
            wrapper.like("notice_title",noticeTitle);
        }
        noticeMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }
}
